package com.tim.helper.string;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static java.nio.charset.StandardCharsets.UTF_8;

public class StrTransformer {
    String str="输出";
    boolean overwriteFlag =false;// true不覆盖，false覆盖

    @Test
    public void urlEncode() throws UnsupportedEncodingException {
        str = overwriteFlag ? str
                : "haha";

        System.out.println(URLEncoder.encode(str, UTF_8.name()));
    }

    @Test
    public void urlDecode() throws UnsupportedEncodingException {
        str = overwriteFlag ? str
                : "haha";

        String decode = URLDecoder.decode(str, UTF_8.name());
        for (int i = 0; decode.contains("%")&&i < 3; i++) {
            decode = URLDecoder.decode(decode, UTF_8.name());
        }
        System.out.println(decode);
    }

    @Test
    public void base64Encode() throws UnsupportedEncodingException {
        str = overwriteFlag ? str
                : "haha";

        System.out.println(Base64.getEncoder().encodeToString(str.getBytes(UTF_8)));
    }

    @Test
    public void base64Decode() throws UnsupportedEncodingException {
        str = overwriteFlag ? str
                : "haha";

        byte[] decode = Base64.getDecoder().decode(str.getBytes(UTF_8));
        System.out.println(new String(decode, UTF_8));
    }

    @Test
    public void unicodeToChinese(){
        // Unicode编码字符串
        str = overwriteFlag ? str
                : "\\u4e2d\\u6587中文";

        // 使用正则表达式替换Unicode编码
        Pattern pattern = Pattern.compile("\\\\u([0-9a-fA-F]{4})");
        Matcher matcher = pattern.matcher(str);
        StringBuffer buffer = new StringBuffer();

        while (matcher.find()) {
            String hex = matcher.group(1);
            int codePoint = Integer.parseInt(hex, 16);
            matcher.appendReplacement(buffer, Character.toString((char) codePoint));
        }
        matcher.appendTail(buffer);

        System.out.println(buffer.toString());  // 输出: 中文

    }

    @Test
    public void  chineseToUnicode (){
        str = overwriteFlag ? str
                : "中文";  // 汉字字符串

        // 将汉字转换为Unicode编码
        StringBuilder sb = new StringBuilder();
        for (char c : str.toCharArray()) {
            sb.append("\\u").append(Integer.toHexString(c | 0x10000).substring(1));
        }

        System.out.println(sb.toString());  // 输出: \u4e2d\u6587
    }

    @Test
    public void isAllChinese(){
        str = overwriteFlag ? str
                : "中文";  // 汉字字符串

        // \u4e00-\u9fa5，简体；\u3400-\u4dbf，繁体
        String regExp="[\\u4e00-\\u9fa5\\u3400-\\u4dbf]+";
        System.out.println(str.matches(regExp));
    }

    @Test
    public void unicodeFileToUTF8() throws IOException {
        String path="";
        String charsetName="";//notepad查看

        Charset charset = StringUtils.isBlank(charsetName) ? UTF_8
            : Charset.forName(charsetName);
        overwriteFlag=true;
        str= Files.readAllLines(Paths.get(path), charset)
                .stream().collect(Collectors.joining());
        unicodeToChinese();
    }
}
